<?php

/**
 * This is the model class for table "{{refund}}".
 *
 * The followings are the available columns in table '{{refund}}':
 * @property integer $zr_id
 * @property string $zo_id
 * @property integer $zm_id
 * @property double $zr_amount
 * @property string $zp_id
 * @property string $zr_title
 * @property string $zr_tel
 * @property integer $zr_type
 * @property string $zr_username
 * @property string $zr_bank
 * @property string $zr_bankcode
 * @property string $zr_content
 * @property string $zr_user_pic
 * @property integer $zr_cmp_time
 * @property string $zr_cmp_re
 * @property string $zr_cmp_pic
 * @property integer $zr_return
 * @property integer $zar_id
 * @property string $zr_address
 * @property integer $zr_zip
 * @property string $zr_exp_code
 * @property string $zr_exp_name
 * @property integer $zr_admin_id
 * @property string $zr_adm_content
 * @property integer $zr_adm_time
 * @property integer $zr_pay_status
 * @property integer $zr_del
 * @property integer $inputtime
 */
class Refund extends CActiveRecord
{
	//查询时间
	public $limit_time;
	
	//商家会员ID //用于查询
	public $cmpUserId;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Refund the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{refund}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zr_type, zr_kind, zr_cmp_time, zr_return, zar_id, zop_id, zr_zip, zr_admin_id, zr_adm_time, zr_pay_status, zr_del, inputtime,zr_user_time,limit_time', 'numerical', 'integerOnly'=>true),
			array('zr_amount', 'numerical'),
			array('zo_id, zr_tel', 'length', 'max'=>20),
			array('zp_id, zr_bankcode, zr_user_pic, zr_cmp_pic, zr_address, zr_exp_code, zr_exp_name', 'length', 'max'=>255),
			array('zr_title', 'length', 'max'=>250),
			array('zr_username, zr_adm_content', 'length', 'max'=>30),
			array('zr_bank', 'length', 'max'=>100),
			array('zr_content, zr_cmp_re', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zr_id, zo_id, zm_id, zr_amount, zp_id, zr_title, zr_tel, zr_type, zr_username, zr_bank, zr_bankcode, zr_content, zr_user_pic, zr_cmp_time, zr_cmp_re, zr_cmp_pic, zr_return, zar_id, zr_address, zr_zip, zr_exp_code, zr_exp_name, zr_admin_id, zr_adm_content, zr_adm_time, zr_pay_status, zr_del, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'ordproduct'=>array(self::BELONGS_TO,'OrdProduct','zop_id'),
				'order'=>array(self::BELONGS_TO,'Order','zo_id'),
				'buyer'=>array(self::BELONGS_TO,'Member','zm_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zr_id' => 'Zr',
			'zo_id' => 'Zo',
			'zm_id' => 'Zm',
			'zr_amount' => 'Zr Amount',
			'zp_id' => 'Zp',
			'zop_id' => 'ZoP',
			'zr_title' => 'Zr Title',
			'zr_tel' => 'Zr Tel',
			'zr_type' => 'Zr Type',
			'zr_username' => 'Zr Username',
			'zr_bank' => 'Zr Bank',
			'zr_bankcode' => 'Zr Bankcode',
			'zr_content' => 'Zr Content',
			'zr_user_pic' => 'Zr User Pic',
			'zr_cmp_time' => 'Zr Cmp Time',
			'zr_cmp_re' => 'Zr Cmp Re',
			'zr_cmp_pic' => 'Zr Cmp Pic',
			'zr_return' => 'Zr Return',
			'zr_user_time'=>'买家处理时间',
			'zar_id' => 'Zar',
			'zr_address' => 'Zr Address',
			'zr_zip' => 'Zr Zip',
			'zr_exp_code' => 'Zr Exp Code',
			'zr_exp_name' => 'Zr Exp Name',
			'zr_admin_id' => 'Zr Admin',
			'zr_adm_content' => 'Zr Adm Content',
			'zr_adm_time' => 'Zr Adm Time',
			'zr_pay_status' => 'Zr Pay Status',
			'zr_del' => 'Zr Del',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zr_id',$this->zr_id);
		$criteria->compare('zo_id',$this->zo_id,true);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zr_amount',$this->zr_amount);
		$criteria->compare('zp_id',$this->zp_id,true);
		$criteria->compare('zr_title',$this->zr_title,true);
		$criteria->compare('zr_tel',$this->zr_tel,true);
		$criteria->compare('zr_type',$this->zr_type);
		$criteria->compare('zr_username',$this->zr_username,true);
		$criteria->compare('zr_bank',$this->zr_bank,true);
		$criteria->compare('zr_bankcode',$this->zr_bankcode,true);
		$criteria->compare('zr_content',$this->zr_content,true);
		$criteria->compare('zr_user_pic',$this->zr_user_pic,true);
		$criteria->compare('zr_cmp_time',$this->zr_cmp_time);
		$criteria->compare('zr_cmp_re',$this->zr_cmp_re,true);
		$criteria->compare('zr_cmp_pic',$this->zr_cmp_pic,true);
		$criteria->compare('zr_return',$this->zr_return);
		$criteria->compare('zr_user_time',$this->zr_user_time);
		$criteria->compare('zar_id',$this->zar_id);
		$criteria->compare('zr_address',$this->zr_address,true);
		$criteria->compare('zr_zip',$this->zr_zip);
		$criteria->compare('zr_exp_code',$this->zr_exp_code,true);
		$criteria->compare('zr_exp_name',$this->zr_exp_name,true);
		$criteria->compare('zr_admin_id',$this->zr_admin_id);
		$criteria->compare('zr_adm_content',$this->zr_adm_content,true);
		$criteria->compare('zr_adm_time',$this->zr_adm_time);
		$criteria->compare('zr_pay_status',$this->zr_pay_status);
		$criteria->compare('zr_del',$this->zr_del);
		
		$criteria->compare('zr_kind',$this->zr_kind);
		
		if(!empty($this->cmpUserId)){//商家ID
			$criteria->join = 'LEFT JOIN {{order}} o ON o.zo_id=t.zo_id ';
			$criteria->addCondition('o.zm_id_cmp ='.$this->cmpUserId);
		}
		
		//根据时间查询
		if(!empty($this->limit_time)){
			if($this->limit_time==1){
				$criteria->addCondition(' inputtime  >'.(time()-3600*24*30));
			}elseif($this->limit_time==2){
				$criteria->addCondition(' inputtime  >'.(time()-3600*24*30*3));
			}elseif($this->limit_time==3){
				$criteria->addCondition(' inputtime  <'.(time()-3600*24*30*3));
			}
		}else{
			$criteria->compare('inputtime',$this->inputtime);
		}
		
		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>Array(
				'pageSize'=>9,	
				),
		));
	}

	//获取模型验证失败信息，返回字符串
	public function getErrorsStr(){
		header("Content-type:text/html;charset=utf-8");
		$errors = $this->getErrors();
		$message = '对不起，数据验证失败。信息如下：';
		foreach($errors as $key=>$value){
			$message .= '<br/>'.$key.'：';
			foreach($value as $key1=>$value1){
				$message .= $key1+1;
				$message .= '.'.$value1;
			}
			return $message;
		}
	}
	
	//反序列化图片数组
	public function afterFind(){
		parent::afterFind();
		$this->zr_user_pic = unserialize($this->zr_user_pic);
		$this->zr_cmp_pic = unserialize($this->zr_cmp_pic);
	}
	
	//列化图片数组
	public function beforeSave(){
		$this->zr_user_pic = serialize($this->zr_user_pic);
		$this->zr_cmp_pic = serialize($this->zr_cmp_pic);
		return true;
	}
	
	//根据订单实例生成订单状态
	public function createOrderStatus($order){
		$status = '';
		if($order->zo_exp_status==1){
			$status = 1;
		}elseif($order->zo_exp_status==2){
			$status = 2;
		}elseif($order->zo_exp_status==5){
			$status = 3;
		}
		return $status;
	}		

	//列表数据
	public function listSearch()
	{
		$criteria=new CDbCriteria;
		//售后或退款
		$kind = Yii::app()->request->getParam('kind',1);
		$criteria->compare('zr_kind',$kind);
		//订单id
		$orderid = Yii::app()->request->getParam('orderid');
		$criteria->compare('t.zo_id',$orderid);
		//时间
		$time = Yii::app()->request->getParam('time');
		switch($time){
			case 1 :$timestr = '>'.(time()-3600*24*90);
					$criteria->compare('inputtime',$timestr);
			break;
			case 2 :$timestr = '<'.(time()-3600*24*90);
					$criteria->compare('inputtime',$timestr);
			break;
			default :;
			break;
		}
		
		$criteria->compare('zr_del',1);
		$criteria->compare('t.zm_id',Yii::app()->user->userid);

		$criteria->with = 'ordproduct';
		$criteria->order = 'inputtime desc';
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination'=>array('pageSize'=>10),
		));
	}
	
	/**
	 *退款状态 
	 */
	public function getPayStatus($type=null){
		if($type==null){
			return array(
				'1'=>'申请退款',
				'2'=>'退款失败',
				'3'=>'退款处理中',
				'4'=>'退款成功',
				'5'=>'撤消退款',
			);
		}else{
			$level = $this->getPayStatus();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 *退款原因 
	 */
	public function getReason($type=null){
		if($type==null){
			return array(
					'1'=>'不想买了',
					'2'=>'暂时缺货',
					'3'=>'双方协商退款',
					'4'=>'未收到货',
					'5'=>'质量问题',
					'6'=>'商品不满意',
					'7'=>'其他',
			);
		}else{
			$level = $this->getReason();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 *成交时间
	 */
	public function getLimitTime($type=null){
		if($type==null){
			return array(
					''=>'请选择',
					'1'=>'最近一个月',
					'2'=>'最近三个月',
					'3'=>'三个月之前',
			);
		}else{
			$level = $this->getLimitTime();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
}